package com.masterclient.app.modules.shop.adapter;

import android.app.Activity;
import android.graphics.Rect;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.masterclient.app.R;
import com.masterclient.app.base.MainViewHolder;
import com.masterclient.app.modules.shop.beans.StoreRecommendBean;
import com.masterclient.app.modules.shop.beans.StoreRecommendGroupBean;

import java.util.List;

/**
 * @auther hjian
 * @date 2020/4/9.
 */
public class StoreRecommendLayoutAdapter extends DelegateAdapter.Adapter<MainViewHolder> {
    private Activity mContext;
    private LayoutHelper mLayoutHelper;
    private VirtualLayoutManager.LayoutParams mLayoutParams;
    private int count = 0;
    private String title;
    private List<StoreRecommendBean> data;

    public StoreRecommendLayoutAdapter(Activity context, LayoutHelper layoutHelper, int count, @NonNull VirtualLayoutManager.LayoutParams layoutParams,StoreRecommendGroupBean data) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.count = count;
        this.mLayoutParams = layoutParams;
        this.title = data.getText();
        this.data = data.getProducts();
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @NonNull
    @Override
    public MainViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MainViewHolder(LayoutInflater.from(mContext).inflate(R.layout.store_hotsale, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MainViewHolder holder, int position) {
        if (mLayoutParams != null) {
            holder.itemView.setLayoutParams(
                    new VirtualLayoutManager.LayoutParams(mLayoutParams));
        }

        ((TextView) holder.itemView.findViewById(R.id.store_hotsale_title)).setText(title);
        RecyclerView recyclerView = holder.itemView.findViewById(R.id.store_hotsale_recycler);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        if (recyclerView.getItemDecorationCount() <= 0) {
            recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
                @Override
                public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                    super.getItemOffsets(outRect, view, parent, state);
                    outRect.set(0, 0, 0, ScreenUtils.dp2px(12));
                }
            });
        }

        StoreRecommendAdapter recommendAdapter = new StoreRecommendAdapter(RecyclerView.VERTICAL);
        recommendAdapter.bindToRecyclerView(recyclerView);
        recyclerView.setAdapter(recommendAdapter);

        recommendAdapter.setNewData(data);

    }

    public void setData(StoreRecommendGroupBean data) {
        this.data = data.getProducts();
        this.title = data.getText();
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return count;
    }


    @Override
    public int getItemViewType(int position) {
        return 3;
    }
}
